#Beef1
#Testiramo kolicino vhodnih plasmidov in gledamo zacetno stanje, v katero se prelevi. Zaradi vecje robustnosti kontrolirano po dva in dva;
#Konfiguracija za preklop;
#Koliko vec enega plasmida je potrebno, da pridemo v njegovo stanje?

from jsmodel_bistable_switch_symmetric_final import *
from pylab import plot
from pylab import show
from pylab import legend
from pylab import figure
from pylab import semilogy

import datetime
import os
dir = "./beef/"
fname = dir+(sys.argv[0].split('.')[0] + "/"+ "".join(str(datetime.datetime.now()).split())).replace(':','').replace('.','').replace('-','') + "_" + str(sys.argv[0]) + "_log.txt"
pname = dir+(sys.argv[0].split('.')[0] + "/"+ "".join(str(datetime.datetime.now()).split())).replace(':','').replace('.','').replace('-','') + "_" + str(sys.argv[0]) + "_plot"
print datetime.datetime.now()
fp = open(fname, 'w')

BIF_P1 = linspace(100,500,int(sys.argv[1]))
BIF_P2 = linspace(100,500,int(sys.argv[1]))


fa = figure()
i = 1
r_old = 0;
for pp1 in BIF_P1:
  for pp2 in BIF_P2:
 

    t = 5000
    p1 = int(pp1)
    p2 = int(pp2)
    p3 = 200
    p4 = 200
    p5 = 200
    p6 = 200
    pcs = []
    pchl = 0
    pcd = [0]
    rgs = []
    rghl = 0
    rgd = [0]
    thresh = 1e2
    req = not True
    #Hill coefficient: inducer 1, inducer2, tal97, tal57
    hk = [2.0,2.0,3.0,3.0]
    switch = BistableSwitch(True, t, p1, p2, p3, p4, p5, p6, req, pcs,  pchl, pcd, rgs, rghl, rgd, hk, thresh)
    r = switch.run()
    #r = int(random()*4)-1
            
    if not r == r_old:
      lstring = "{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16} *\n"
      lstring = lstring.format(t, p1, p2, p3, p4, p5, p6, req, pcs,  pchl, pcd, rgs, rghl, rgd, hk, thresh, r)
    else:
      lstring = "{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16} \n"
      lstring = lstring.format(t, p1, p2, p3, p4, p5, p6, req, pcs,  pchl, pcd, rgs, rghl, rgd, hk, thresh, r)
    fp.write(lstring)
    
    r_old = r;
    
    i = i + 1
    if i % 1000 == 0:
      print i
    
    if r == 0:
      plot(p1,p2, 'gs', label='BISTABLE')
    elif r == 1:
      plot(p1,p2, 'ys', label='MONOSTATE 57')
    elif r == 2:
      plot(p1,p2, 'bs', label='MONOSTATE 97')
    elif r == 3:
      plot(p1,p2, 'rs', label='AMBIGOUS')
    elif r == -1:
      plot(p1,p2, 'ks', label='NOT VALID')
    

fp.close()
xlabel("Plasmid #1 dosage [ng]")  
ylabel("Plasmid #2 dosage [ng]")
savefig(pname+".png", format ='png')
savefig(pname+".svg", format ='svg')
print datetime.datetime.now()
quit()

